package com.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.reggie.configuration.BaseContext;
import com.reggie.configuration.R;
import com.reggie.pojo.Orders;
import com.reggie.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/order")
@Slf4j
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    /**
     * 用户下单
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        log.info("用户下单");
        ordersService.submit(orders);
        return R.success("下单成功");
    }

    /**
     * 查看订单
     */
    @GetMapping("/userPage")
    public R<Page<Orders>> page(Integer page,Integer pageSize){
        //构造分页构造器
        Page<Orders> ordersPage = new Page<Orders>(page, pageSize);
        //构造条件构造器
        LambdaQueryWrapper<Orders> ordersLambdaQueryWrapper = new LambdaQueryWrapper<>();
        ordersLambdaQueryWrapper.eq(Orders::getUserId, BaseContext.getCurrentId());
        Page<Orders> orders = ordersService.page(ordersPage, ordersLambdaQueryWrapper);

        return R.success(orders);
    }
}
